package cn.iocoder.yudao.module.jifenyuan.controller.app.room;

import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jifenyuan.controller.app.roomuserbind.vo.AppRoomUserBindRespVO;
import cn.iocoder.yudao.module.jifenyuan.controller.app.roomuserbind.vo.AppRoomUserBindWithInfoRespVO;
import cn.iocoder.yudao.module.jifenyuan.convert.roomuserbind.RoomUserBindConvert;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.jifenyuan.controller.app.room.vo.*;
import cn.iocoder.yudao.module.jifenyuan.dal.dataobject.room.RoomDO;
import cn.iocoder.yudao.module.jifenyuan.convert.room.RoomConvert;
import cn.iocoder.yudao.module.jifenyuan.service.room.RoomService;

@Tag(name = "用户 APP - 记分员 房间")
@RestController
@RequestMapping("/jifenyuan/room")
@Validated
public class AppRoomController {

    @Resource
    private RoomService roomService;

    @PostMapping("/create")
    @Operation(summary = "创建记分员 房间")
    @PreAuthenticated
    public CommonResult<AppRoomRespVO> createRoom(@Valid @RequestBody AppRoomCreateReqVO createReqVO) {
        AppRoomRespVO convert = RoomConvert.INSTANCE.convert(roomService.createRoom(createReqVO));
        return success(convert);
    }


    @GetMapping("/getLeaderRoom")
    @Operation(summary = "获取当前创建的没有关闭的房间")
    @PreAuthenticated
    public CommonResult<AppRoomRespVO> getLeaderRoom() {
        AppRoomRespVO convert = RoomConvert.INSTANCE.convert(roomService.selectCurrentLeaderRoom());
        return success(convert);
    }

    @PostMapping("/checkRoom")
    @Operation(summary = "判断是否再房间内")
    @PreAuthenticated
    public CommonResult<AppRoomWithBindTypeRespVO> checkRoom() {
        AppRoomWithBindTypeRespVO convert = RoomConvert.INSTANCE.convert4(roomService.checkInRoom());
        return success(convert);
    }

    @PostMapping("/leaveCurrentRoom")
    @Operation(summary = "离开房间")
    @PreAuthenticated
    public CommonResult<Boolean> leaveCurrentRoom() {
        roomService.leaveCurrentRoom();
        return success(true);
    }

    @PostMapping("/getRoomShareImg")
    @Operation(summary = "获取当前房间分享码")
    @PreAuthenticated
    public CommonResult<String> getRoomShareImg() {
        String roomShareImg = roomService.getRoomShareImg();
        return success(roomShareImg);
    }

    @GetMapping("/closeRoom")
    @Operation(summary = "加入房间")
    @PreAuthenticated
    public CommonResult<Boolean> closeRoom() {
        roomService.closeRoom();
        return success(true);
    }

    @GetMapping("/hideSelf")
    @Operation(summary = "加入房间")
    @PreAuthenticated
    public CommonResult<Boolean> hideSelf() {
        roomService.hideSelf();
        return success(true);
    }


    @PostMapping("/getRoomObserveImg")
    @Operation(summary = "获取当前房间观察码")
    @PreAuthenticated
    public CommonResult<String> getRoomObserveImg() {
        String roomShareImg = roomService.getRoomObserveImg();
        return success(roomShareImg);
    }


    @GetMapping("/joinPartnerRoom")
    @Operation(summary = "加入房间")
    @PreAuthenticated
    public CommonResult<AppRoomRespVO> joinPartnerRoom(String inviteCode) {
        AppRoomRespVO convert = RoomConvert.INSTANCE.convert(roomService.joinPartnerRoom(inviteCode));
        return success(convert);
    }
    @GetMapping("/joinObserverRoom")
    @Operation(summary = "观看房间")
    @PreAuthenticated
    public CommonResult<AppRoomRespVO> joinObserverRoom(@RequestParam("code") String code,@RequestParam("cid")String cid,@RequestParam("source")String source) {
        AppRoomRespVO convert = RoomConvert.INSTANCE.convert(roomService.joinObserverRoom(code,cid,source));
        return success(convert);
    }


    @GetMapping("/getRoomPartner")
    @Operation(summary = "获取房间内的参与者")
    @PreAuthenticated
    public CommonResult<List<AppRoomUserBindWithInfoRespVO>> getRoomPartner() {
        List<AppRoomUserBindWithInfoRespVO> appRoomUserBindRespVOS = RoomUserBindConvert.INSTANCE.convertList04(roomService.getRoomPartner());
        return success(appRoomUserBindRespVOS);
    }
    @GetMapping("/getRoomScore")
    @Operation(summary = "获取房间内的参与者积分")
    @PreAuthenticated
    public CommonResult<List<AppRoomUserBindRespVO>> getRoomScore() {
        List<AppRoomUserBindRespVO> appRoomUserBindRespVOS = RoomUserBindConvert.INSTANCE.convertList(roomService.getRoomScore());
        return success(appRoomUserBindRespVOS);
    }

    @PostMapping("/roomTransfer")
    @Operation(summary = "房间内转账")
    @PreAuthenticated
    public CommonResult<Boolean> roomTransfer(@RequestBody @Valid RoomTransferVo roomTransferVo) {
        roomService.roomTransfer(roomTransferVo.getToUserId(),roomTransferVo.getScore());
        return success(true);
    }




//    @PutMapping("/update")
//    @Operation(summary = "更新记分员 房间")
//
//    public CommonResult<Boolean> updateRoom(@Valid @RequestBody AppRoomUpdateReqVO updateReqVO) {
//        roomService.updateRoom(updateReqVO);
//        return success(true);
//    }
//
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除记分员 房间")
//    @Parameter(name = "id", description = "编号", required = true)
//
//    public CommonResult<Boolean> deleteRoom(@RequestParam("id") Long id) {
//        roomService.deleteRoom(id);
//        return success(true);
//    }
//
//    @GetMapping("/get")
//    @Operation(summary = "获得记分员 房间")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//
//    public CommonResult<AppRoomRespVO> getRoom(@RequestParam("id") Long id) {
//        RoomDO room = roomService.getRoom(id);
//        return success(RoomConvert.INSTANCE.convert(room));
//    }
//
//    @GetMapping("/list")
//    @Operation(summary = "获得记分员 房间列表")
//    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
//
//    public CommonResult<List<AppRoomRespVO>> getRoomList(@RequestParam("ids") Collection<Long> ids) {
//        List<RoomDO> list = roomService.getRoomList(ids);
//        return success(RoomConvert.INSTANCE.convertList(list));
//    }
//
//    @GetMapping("/page")
//    @Operation(summary = "获得记分员 房间分页")
//
//    public CommonResult<PageResult<AppRoomRespVO>> getRoomPage(@Valid AppRoomPageReqVO pageVO) {
//        PageResult<RoomDO> pageResult = roomService.getRoomPage(pageVO);
//        return success(RoomConvert.INSTANCE.convertPage(pageResult));
//    }
//
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出记分员 房间 Excel")
//
//    @OperateLog(type = EXPORT)
//    public void exportRoomExcel(@Valid AppRoomExportReqVO exportReqVO,
//              HttpServletResponse response) throws IOException {
//        List<RoomDO> list = roomService.getRoomList(exportReqVO);
//        // 导出 Excel
//        List<AppRoomExcelVO> datas = RoomConvert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "记分员 房间.xls", "数据", AppRoomExcelVO.class, datas);
//    }

}
